﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;

//2010-04-09增加
using IBatisNet.DataMapper;
using IBatisNet.Utilities;
using IBatisNet.DataMapper.Configuration;
using System.Collections.Specialized;
using IBatisNet.Common.Utilities;
using IBatisNet.DataAccess.Configuration;

namespace LDAP
{
    public class SqlServerCommon
    {
        /// <summary>
        /// 框架连接串
        /// </summary>
        public static readonly string SqlConnStr = ConfigurationSettings.AppSettings["SqlConnStr2"] ?? "";

        #region Fields
        private static volatile ISqlMapper _mapper = null;
        #endregion

        public static void Configure(object obj)
        {
            _mapper = null;
        }

        /// <summary>
        /// Init the 'default' SqlMapper defined by the SqlMap.Config file.
        /// </summary>
        public static void InitMapper()
        {
            ConfigureHandler handler = new ConfigureHandler(Configure);
            DomSqlMapBuilder builder = new DomSqlMapBuilder();
            _mapper = builder.ConfigureAndWatch(handler);
        }

        /// <summary>
        /// Get the instance of the SqlMapper defined by the SqlMap.Config file.
        /// </summary>
        /// <returns>A SqlMapper initalized via the SqlMap.Config file.</returns>
        public static ISqlMapper Instance()
        {
            if (_mapper == null)
            {
                lock (typeof(SqlMapper))
                {
                    if (_mapper == null) // double-check
                    {
                        InitMapper();
                    }
                }
            }
            return _mapper;
        }

        /// <summary>
        /// Get the instance of the SqlMapper defined by the SqlMap.Config file. (Convenience form of Instance method.)
        /// </summary>
        /// <returns>A SqlMapper initalized via the SqlMap.Config file.</returns>
        public static ISqlMapper Get()
        {
            return Instance();
        }


        /// <summary>
        /// 默认的mapper
        /// </summary>
        public static ISqlMapper DefaultMapper
        {
            get
            {
                const string key = "my_mapper2";
                ISqlMapper old = SqlMapperHelper.Instance.Get(key);
                if (old != null)
                {
                    return old;
                }

                DomSqlMapBuilder builder = new DomSqlMapBuilder();
                NameValueCollection prop = new NameValueCollection();
                prop.Add("connectionString", SqlConnStr);
                builder.Properties = prop;
                ISqlMapper _sqlMapper = builder.Configure("sqlservermap.config");
                return SqlMapperHelper.Instance.Add(key, _sqlMapper);
            }
        }

        public static ISqlMapper GetMapper(string key, object value)
        {
            return DefaultMapper;
        }
    }
}
